package com.leyou.service.impl;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.mapper.ISpecMapper;
import com.leyou.mapper.ISpecParamMapper;
import com.leyou.service.ISpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xiuer
 * @description 规格分组业务层
 * @create 2019/6/26 20:51
 */
@Service
public class SpecServiceImpl implements ISpecService {
    @Autowired
    private ISpecMapper specMapper;
    @Autowired
    private ISpecParamMapper specParamMapper;

    /**
     * 根据分类id查询规格组信息
     *
     * @param cid 分类id
     * @return
     */
    @Override
    public List<SpecGroupDTO> querySpecGroupByid(Long cid) {
        List<SpecGroup> list = specMapper.querySpecGroupByid(cid);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, SpecGroupDTO.class);
    }


    /**
     * 新增规格组
     *
     * @param specGroupDTO
     */
    @Override
    public void insertSpecGroup(SpecGroupDTO specGroupDTO) {
        int affectRows = specMapper.insertSpecGroup(specGroupDTO);
        if (affectRows == 0) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }


    /**
     * 根据规格组id查询规格参数信息
     *
     * @param gid       规格组id
     * @param cid
     * @param searching
     * @return
     */
    @Override
    public List<SpecParamDTO> querySepcParams(Long gid, Long cid, Boolean searching) {
        if (gid == null && cid == null && searching == null) {
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        List<SpecParam> list = specParamMapper.querySpecParamByGid(gid, cid, searching);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, SpecParamDTO.class);
    }

    /**
     * 新增规格参数
     *
     * @param specParamDTO
     */
    @Override
    public void insertSpecParam(SpecParamDTO specParamDTO) {
        SpecParam specParam = BeanHelper.copyProperties(specParamDTO, SpecParam.class);
        int affectRows = specParamMapper.insertSpecParam(specParam);
        if (affectRows == 0) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    /**
     * 修改规格组参数
     *
     * @param specParamDTO
     */
    @Override
    public void updateSepcParam(SpecParamDTO specParamDTO) {
        SpecParam specParam = BeanHelper.copyProperties(specParamDTO, SpecParam.class);
        int affectRows = specParamMapper.updateSepcParam(specParam);
        if (affectRows == 0) {
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    /**
     * 根据分类id查询规格组和规格参数 并将规格参数封装到规格组中
     *
     * @param cid
     * @return
     */
    @Override
    public List<SpecGroupDTO> querySpecGroupAndParams(Long cid) {
        List<SpecParamDTO> specParams = querySepcParams(null, cid, null);

      //  Map<Long, List<SpecParamDTO>> params = new HashMap<>();
      //  //创建Map来分组
      //  for (SpecParamDTO param : specParams) {
      //      //判断是不是第一个 ，是则创建几个
      //      if (!params.containsKey(param.getGroupId())) {
      //          params.put(param.getGroupId(), new ArrayList<>());
      //      } else {
      //          //不是 则把自己添加到集合中去
      //          params.get(param.getGroupId()).add(param);
      //      }
      //  }
        //stream流方式简写
        Map<Long, List<SpecParamDTO>> params = specParams.stream()
                .collect(Collectors.groupingBy(SpecParamDTO::getGroupId));
        List<SpecGroupDTO> specGroupDTOS = querySpecGroupByid(cid);
        for (SpecGroupDTO specGroupDTO : specGroupDTOS) {
            specGroupDTO.setParams(params.get(specGroupDTO.getId()));
        }
        return specGroupDTOS;
    }
}
